<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\MemberModel;
use Model\SettingFinanceModel;
use Model\W7McMappingFansModel;
use Model\W7McMembersModel;

/**
 * 会员
 * Class Member
 * @package Controller
 */
class Member extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 注册用户
     */
    public function register()
    {
        if (!MemberModel::where("uid", $this->_uid)->where("uniacid", $this->_uniacid)->value("id")) {
            $_data = array(
                "uniacid" => $this->_uniacid,
                "uid" => $this->_uid
            );
            return MemberModel::create($_data);
        } else {
            return false;
        }
    }

    /**
     * 获取指定用户余额
     * @param int $_uid
     * @return float 返回用户余额
     */
    public function finance($_uid = 0)
    {
        if (!$_uid) $_uid = $this->_uid;
        if (SettingFinanceModel::where("uniacid", $this->_uniacid)->cache(!KUYUAN_DEBUG)->value("use_balance") == 1) {
            // 独立余额
            $_finance = MemberModel::where("uid", $_uid)->value("balance");
        } else {
            // 微擎通用余额
            $_finance = W7McMembersModel::where("uid", $_uid)->value("credit2");
        }
        return $_finance;
    }

    /**
     * 操作用户余额
     * @param float $_balance 操作的余额
     * @param boolean $_type true = 新增余额 false=减去余额
     * @param int $_uid
     * @return mixed
     */
    public function financeHandle($_balance, $_type, $_uid = 0)
    {
        if (!$_uid) $_uid = $this->_uid;
        if (SettingFinanceModel::where("uniacid", $this->_uniacid)->value("use_balance") == 1) {
            // 独立余额
            $MemberModel = MemberModel::where("uid", $_uid);
            return $_type ? $MemberModel->addition("balance", $_balance) : $MemberModel->minus("balance", $_balance);
        } else {
            // 微擎通用余额
            return mc_credit_update($_uid, "credit2", $_type ? '+' : '-' . $_balance);
        }
    }

    /**
     * uid请求openid
     * @param int $_uid 用户id
     * @return mixed
     */
    public function uidToOpenid($_uid = 0)
    {
        if (!$_uid) $_uid = $this->_uid;
        return W7McMappingFansModel::where("uid", $_uid)->cache(!KUYUAN_DEBUG)->value("openid");
    }

    /**
     * 获取一个用户的昵称和头像
     * @param int $_uid 用户id
     * @return mixed
     */
    public function findNameHeader($_uid = 0)
    {
        if($_uid == 0) $_uid = $this->_uid;
        return W7McMembersModel::where("uid",$_uid)
            ->field(array("nickname","avatar"))
            ->cache(!KUYUAN_DEBUG)
            ->find();
    }

    /**
     * 后台获取用户列表
     * @param array $_limit
     * @return mixed
     */
    public function getList(Array $_limit)
    {
        return MemberModel::where("uniacid",$this->_uniacid)
            ->field(array("uid","register_time","balance"))
            ->getAttribute("register_time,red_number,nickname,avatar")
            ->order("id","desc")
            ->limit($_limit[0],$_limit[1])
            ->select();
    }

    /**
     * 获取会员总数
     */
    public function getTotal()
    {
        return MemberModel::where("uniacid",$this->_uniacid)
            ->count();
    }

    /**
     * 获取指定日期新增的用户
     * @param string $_date 日期 格式 2020-10-20
     * @return mixed
     */
    public function getDayMember($_date = "")
    {
        if(empty($_date)) {
            $_start_time = strtotime(date("Y-m-d",TIMESTAMP));
            $_end_time = $_start_time + 86400;
        } else {
            $_start_time = strtotime($_date);
            $_end_time = $_start_time + 86400;
        }
        return MemberModel::where("uniacid",$this->_uniacid)
            ->where("register_time",">=",$_start_time)
            ->where("register_time","<",$_end_time)
            ->count();
    }
}